package com.xiaoyu.lanling.feature.mission.viewholder

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import com.facebook.drawee.view.SimpleDraweeView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.feature.checkin.CheckInDialogFragment
import com.xiaoyu.lanling.feature.checkin.datamodel.CheckInInfo
import com.xiaoyu.lanling.feature.mission.model.MissionItem
import com.xiaoyu.lanling.feature.mission.model.MissionItem.Companion.ACTION_CHECK_IN
import com.xiaoyu.lanling.feature.mission.model.MissionItem.Companion.ACTION_ROUTE
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.deeplink.DeepLinkRouter

class MissionViewHolder : ViewHolderBase<MissionItem>() {

    companion object {
        private val sOnItemClickListener = View.OnClickListener { v ->
            val itemData = v.getViewTag<MissionItem>() ?: return@OnClickListener
            when (itemData.action) {
                ACTION_ROUTE -> {
                    val activity = App.getInstance().topActivity ?: return@OnClickListener
                    DeepLinkRouter.getInstance().dispatch(activity, itemData.url)
                }
                ACTION_CHECK_IN -> {
                    val activity = (App.getInstance().topActivity as? FragmentActivity)
                            ?: return@OnClickListener
                    CheckInDialogFragment.show(activity.supportFragmentManager, CheckInInfo(JsonData.create(itemData.payload)), Constant.From.MISSION)
                }
            }
        }
    }

    private var mIcon: SimpleDraweeView? = null
    private var mTitle: TextView? = null
    private var mDesc: TextView? = null
    private var mAwardDesc: TextView? = null

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.item_mission, parent, false)
        mIcon = view.findViewById(R.id.icon)
        mTitle = view.findViewById(R.id.title)
        mDesc = view.findViewById(R.id.desc)
        mAwardDesc = view.findViewById(R.id.award_desc)
        view.setOnClickDebounceListener(sOnItemClickListener)
        return view
    }

    override fun showData(position: Int, itemData: MissionItem) {
        mIcon?.let {
            ImageViewUtil.loadImage(it, itemData.imageParam)
        }
        mTitle?.text = itemData.title
        mDesc?.text = itemData.desc
        mAwardDesc?.text = itemData.awardDesc
        mAwardDesc?.isEnabled = !itemData.isCompleted
        mAwardDesc?.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, if (itemData.isCompleted) 0 else R.drawable.icon_task_red_package, 0)
        mCurrentView?.isEnabled = !itemData.isCompleted
        mCurrentView.setViewTag(itemData)
    }
}